﻿/*
 *  PirateShare - a tool for fair&legal content sharing among friends.  
 *  see <http://pirateshare.codeplex.com> for latest version.
 *
 *  Copyright (C) 2010 Ilya Elenskiy
 *
 *  This program is free software; you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or 
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace PirateShare
{
    static class CryptoHelper
    {
        public static byte[] Hash(byte[] source)
        {
            SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
            SHA1.ComputeHash(source);
            return SHA1.Hash;
        }

        public static string FileHash(string fileName)
        {
            byte[] Hash;
            string Result = string.Empty;
            string Tmp = string.Empty;

            SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();

            FileStream FN = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 8192);
            SHA1.ComputeHash(FN);
            FN.Close();

            Hash = SHA1.Hash;
            for (int i = 0; i < Hash.Length; i++)
            {
                Tmp = Convert.ToString(Hash[i], 16);
                if (Tmp.Length == 1)
                    Tmp = "0" + Tmp;
                Result += Tmp;
            }
            return Result;
        } 



    }
}
